var express = require('express');
var router = express.Router();
var co=require('co');
var handleErr=require('../libs/handleErr');
var UserModel = require("../models").User;
var ShopModel = require("../models").Shop;
var OrderModel = require("../models").Order;
var CommentModel = require("../models").Comment;
var merge=require('utils-merge')
/* GET users listing. */
router.get('/userBaseInfo', function(req, res, next) {
    co(function *() {
        if(req.user){
            var userBaseInfo = yield UserModel.baseInfo(req.user.name);
            res.json({
                isLogged:true,
                userBaseInfo:userBaseInfo
            })
        }else{
            res.json({
                isLogged:false
            });
        }



    }).catch(handleErr.bind(null, res))
});
router.post('/shop', function(req, res, next) {
    co(function *() {

        //req.user={name:"123"};
        if(req.user){
            var userBaseInfo = yield UserModel.baseInfo(req.user.name);
            var shop=req.body;
            shop=merge(shop,{owner:userBaseInfo._id});
            var shopExist=yield ShopModel.findOne({owner:shop.owner});
            if(!shopExist){
                yield ShopModel.create(shop);
                yield UserModel.findOneAndUpdate({_id: shop.owner}, {role: 1});
                res.json({
                    status:'success'
                })
            }else{
                res.json({
                    status:'false',
                    msg:'已经开店'
                })
            }
        }
    }).catch(handleErr.bind(null, res))
});
router.post('/dish', function(req, res, next) {
    co(function *() {
        if(req.user){
            var userBaseInfo = yield UserModel.baseInfo(req.user.name);
            var dish=req.body;
            var shop=yield ShopModel.findOneAndUpdate({owner:userBaseInfo._id},{"$push":{"dishes":dish}});
            res.json({
                status:'success'
            })
        }
    }).catch(handleErr.bind(null, res))
});
router.get('/addressMatched', function(req, res, next) {
    co(function *() {
        var query={};
        if(req.query.address) {
            query.address = new RegExp(req.query.address);
        }
        var shop=yield ShopModel.find(query).limit(10);
        var result=[];
        for(var i=0;i<shop.length;i++){
            var obj={};
            obj.address=shop[i].address;
            obj.name=shop[i].shop_name;
            result.push(obj);
        }
        res.json(result)
    }).catch(handleErr.bind(null, res))
});
router.get('/order', function(req, res, next) {
    co(function *() {
        var query=req.query;
        var userId=query.userId;
        var phone=query.buyerPhone;
        var buyerAddress=query.buyerAddress;
        console.log(buyerAddress);
        var upDateBuyerField={
            phone:phone,
            address:buyerAddress
        }
        yield UserModel.findOneAndUpdate({_id:userId},{$set: upDateBuyerField})
        console.log(query);
        yield OrderModel.create(query);
        res.json({
            status:'success'
        })
    }).catch(handleErr.bind(null, res))
});
router.delete('/order', function(req, res, next) {
    co(function *() {
        //console.log(req.body);
        yield OrderModel.remove(req.body);
        res.json({
            status:'success'
        })
    }).catch(handleErr.bind(null, res))
});
router.post('/comment', function(req, res, next) {
    co(function *() {
        var query=req.body;
        yield CommentModel.create(query);
        res.json({
            status:'success'
        })
    }).catch(handleErr.bind(null, res))
});


module.exports = router;